<?php
include_once ("../functions_for_filter.php");
$db_name = "filter_words";
$dbcnx = connect_db($db_name);

$threshold = 40;
$suffix_array = array("d", "ed", "eer", "eest", "eing", "er", "est", "ied", "ier", "ies", "iest", "ily", "ly", "es", "ing", "s");

for ($index = 1; $index <= 6; $index++)
{
	$sql = "select count(*) from level" .$index. "_words;";
	$result = mysql_query ($sql, $dbcnx);
	$row = mysql_fetch_row ($result);
	$amount = $row[0];
	echo $amount . "\n";
	//if ($amount == 0)
	//如果levelx表是空，在各个小表中找出exception字段不超过40的单词存进大表，把超过40的删除
	{
		for ($i = 0; $i < sizeof($suffix_array); $i++)
		{
			$tab_name = "level" . $index . "_words_" . $suffix_array[$i];
			$sql = "select * from $tab_name;";
			echo $sql . "\n";
			$result = mysql_query($sql, $dbcnx);
			$sql = "INSERT INTO level" . $index . "_words VALUES ";
			//$delete_sql = "delete from $tab_name where";
			$j = 0;
			while ( $row = mysql_fetch_array($result, MYSQL_BOTH) )
			{
				$temp = $row["exception"];
				if ( $temp < 40 || ($temp == NULL) )
				{
					$sql .= "(\"" . $row["word"] . "\", 0, 0),";
				}
				else
				{
					//delete
					//$j++;
					//$delete_sql .= " word=" .$row["word"] . " or";
				}
			}
			$sql = trim ($sql, ",") . ";";
			//echo $sql . "\n";
			$result = mysql_query ($sql, $dbcnx);
			if ($j > 0)
			{
				$delete_sql = trim ($delete_sql, "or") . ";";
				$result = mysql_query ($delete_sql, $dbcnx);
				echo $delete_sql . "\n";
			}
		}
	}
	/*
	else
	//如果levelx表不是空，在各个小表中找出exception字段超过40的单词，从大表中删除这些单词
	{
		$c = 0;
		for ($i = 0; $i < sizeof($suffix_array); $i++)
		{
			$tab_name = "level" . $index . "_words_" . $suffix_array[$i];
			$sql = "select * from $tab_name;";
			$result = mysql_query($sql, $dbcnx);
			$delete_sql = "delete from level" .$index. "_words where";
			$j = 0;
			while ( $row = mysql_fetch_array($result, MYSQL_BOTH) )
			{
				if ($row["exception"] >= 40)
				{
					$delete_sql .= " word=" .$row["word"] . " or";
					$j++;
				}
				$c++;
			}
			if ($j > 0)
			{
				$delete_sql = trim ($delete_sql, "or") . ";";
				$result = mysql_query ($delete_sql, $dbcnx);
			}
		}
		$tab_name = "level" . $index . "_words_prototype";
		$sql = "select * from $tab_name;";
		$result = mysql_query($sql, $dbcnx);
		$sql = "INSERT INTO level" . $index . "_words VALUES ";
		while ( $row = mysql_fetch_array($result, MYSQL_BOTH) )
		{
			$sql .= "(\"" . $row["word"] . "\", 0, 0),";
			$c++;
		}
		$sql = trim ($sql, ",") . ";";
		$result = mysql_query ($sql, $dbcnx);

		$tab_name = "level" . $index . "_words_irr";
		$sql = "select * from $tab_name;";
		$result = mysql_query($sql, $dbcnx);
		$sql = "INSERT INTO level" . $index . "_words VALUES ";
		while ( $row = mysql_fetch_array($result, MYSQL_BOTH) )
		{
			$sql .= "(\"" . $row["word"] . "\", 0, 0),";
			$c++;
		}
		$sql = trim ($sql, ",") . ";";
		echo "level" . $index . " has " . $c . " words\n";
		$result = mysql_query ($sql, $dbcnx);
	}
	*/
}

mysql_close($dbcnx);

?>
